from selenium import webdriver
import time

class Commonshare (object):
    # 初始化
    def get_browser(self):
        # 创建浏览器对象
        self.browser = webdriver.Chrome()
        # 浏览器最大化
        self.browser.maximize_window()
        return self.browser

    # 打开浏览器url地址
    def open_url(self,url):
        #打开网页
        self.browser.get(url)
        self.browser.implicitly_wait(6)

    # 关闭浏览器网页
    def close_url(self):
        time.sleep(4)
        self.browser.close()

    # 定位元素操作
    def locate_element(self,locate_type,locate_value):
        el=None
        if locate_type == "id":
            el=self.browser.find_element_by_id(locate_value)
        elif locate_type == "name":
            el=self.browser.find_element_by_name(locate_value)
        elif locate_type == "class":
            el=self.browser.find_element_by_class_name(locate_value)
        elif locate_type == "text":
            el=self.browser.find_element_by_partial_link_text(locate_value)
        elif locate_type == "xpath":
            el=self.browser.find_element_by_xpath(locate_value)
        else:
            el = self.browser.find_element_by_css_selector(locate_value)
        return el
    #点击元素
    def single_click(self,locate_type,locate_value):
        el=self.locate_element(locate_type,locate_value)
        el.click()
        time.sleep(2)

    #输入内容
    def input_date(self,locate_type,locate_value,value):
       el=self.locate_element(locate_type,locate_value)
       el.send_keys(value)
       time.sleep(2)

